package com.tech.connect.activity;

import android.os.Bundle;
import android.view.View;

import com.gyf.barlibrary.ImmersionBar;
import com.tech.connect.R;
import com.tech.connect.fragment.MainTabContactFragment;
import com.tech.connect.fragment.MainTabFindFragment;
import com.tech.connect.fragment.MainTabHomeFragment;
import com.tech.connect.fragment.MainTabMessageFragment;
import com.tech.connect.fragment.MainTabMineFragment;
import com.tech.connect.util.CatUtils;
import com.tech.connect.view.MenuTabView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private int currentIndex = -1;
    private List<MenuTabView> tabViewList;
    private MainTabHomeFragment mainTabHomeFragment;
    private MainTabMessageFragment mainTabMessageFragment;
    private MainTabContactFragment mainTabContactFragment;
    private MainTabFindFragment mainLighterFragment;
    private MainTabMineFragment mainTabMineFragment;
    private long mLastClickTime;

    @Override
    protected void initImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar
                .fitsSystemWindows(false)
                .statusBarDarkFont(true)
                .keyboardEnable(true)
                .init();
    }

    @Override
    protected boolean isShowHeadBar() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        CatUtils.getInstance().init(this);
    }

    private void initView() {
        MenuTabView tab0 = findViewById(R.id.tab0);
        MenuTabView tab1 = findViewById(R.id.tab1);
        MenuTabView tab2 = findViewById(R.id.tab2);
        MenuTabView tab3 = findViewById(R.id.tab3);
        MenuTabView tab4 = findViewById(R.id.tab4);
        tab0.setTab(MenuTabView.TabMenu.Home);
        tab1.setTab(MenuTabView.TabMenu.Message);
        tab2.setTab(MenuTabView.TabMenu.Contact);
        tab3.setTab(MenuTabView.TabMenu.Lighter);
        tab4.setTab(MenuTabView.TabMenu.User);
        tabViewList = new ArrayList<>();
        tabViewList.add(tab0);
        tabViewList.add(tab1);
        tabViewList.add(tab2);
        tabViewList.add(tab3);
        tabViewList.add(tab4);
        for (int i = 0; i < tabViewList.size(); i++) {
            MenuTabView menuTabView = tabViewList.get(i);
            menuTabView.setTag(i);
            menuTabView.setOnClickListener(this);
        }
        tabViewList.get(0).performClick();
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag != null && tag instanceof Integer) {
            int index = (int) tag;
            if (currentIndex == index) {
                return;
            }
            currentIndex = index;
            for (int i = 0; i < tabViewList.size(); i++) {
                MenuTabView menuTabView = tabViewList.get(i);
                if (currentIndex == i) {
                    menuTabView.setSelected(true);
                } else {
                    menuTabView.setSelected(false);
                }
            }
            showFragment(currentIndex);
        }
    }

    private void showFragment(int index) {
        if (index == 0) {
            if (mainTabHomeFragment == null) {
                mainTabHomeFragment = new MainTabHomeFragment();
                addFragment(R.id.container, mainTabHomeFragment);
            }
            showFragment(mainTabHomeFragment);
            hideFragment(mainTabMessageFragment);
            hideFragment(mainTabContactFragment);
            hideFragment(mainLighterFragment);
            hideFragment(mainTabMineFragment);
        } else if (index == 1) {
            if (mainTabMessageFragment == null) {
                mainTabMessageFragment = new MainTabMessageFragment();
                addFragment(R.id.container, mainTabMessageFragment);
            }
            hideFragment(mainTabHomeFragment);
            showFragment(mainTabMessageFragment);
            hideFragment(mainTabContactFragment);
            hideFragment(mainLighterFragment);
            hideFragment(mainTabMineFragment);
        } else if (index == 2) {
            if (mainTabContactFragment == null) {
                mainTabContactFragment = new MainTabContactFragment();
                addFragment(R.id.container, mainTabContactFragment);
            }
            hideFragment(mainTabHomeFragment);
            hideFragment(mainTabMessageFragment);
            showFragment(mainTabContactFragment);
            hideFragment(mainLighterFragment);
            hideFragment(mainTabMineFragment);
        } else if (index == 3) {
            if (mainLighterFragment == null) {
                mainLighterFragment = new MainTabFindFragment();
                addFragment(R.id.container, mainLighterFragment);
            }
            hideFragment(mainTabHomeFragment);
            hideFragment(mainTabMessageFragment);
            hideFragment(mainTabContactFragment);
            showFragment(mainLighterFragment);
            hideFragment(mainTabMineFragment);
        } else if (index == 4) {
            if (mainTabMineFragment == null) {
                mainTabMineFragment = new MainTabMineFragment();
                addFragment(R.id.container, mainTabMineFragment);
            }
            hideFragment(mainTabHomeFragment);
            hideFragment(mainTabMessageFragment);
            hideFragment(mainTabContactFragment);
            hideFragment(mainLighterFragment);
            showFragment(mainTabMineFragment);
        }
    }

    @Override
    public void onBackPressed() {
        final long now = System.currentTimeMillis();
        if (now - mLastClickTime > 1000) {
            mLastClickTime = now;
            showToast("再按一次退出连程");
        } else {
            super.onBackPressed();
        }
    }
}
